package com.bnz.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;


@WebFilter(urlPatterns = "/*",initParams = {@WebInitParam(name = "encoding",value = "UTF-8")})
public class CharacterEncodingFilter implements Filter {

	private String encoding;
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		encoding = filterConfig.getInitParameter("encoding");
		System.out.println("CharacterEncodingFilter init...");
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		System.out.println("CharacterEncodingFilter doFilter...");
		servletRequest.setCharacterEncoding(encoding);
		servletResponse.setContentType("text/html;charset=utf-8");
		filterChain.doFilter(servletRequest,servletResponse);
	}

	@Override
	public void destroy() {
		System.out.println("CharacterEncodingFilter destroy...");
	}
}
